#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 14 14:33:41 2021

@author: liqingsimac
"""


# 7.2. 
'''
import sympy as sy
sy.init_printing()
x,y = sy.symbols('x y')
D = (x+y)*sy.exp(x)*sy.cos(y)
type(x)
type(y)
type(D)
'''
'''
import sympy as sy
x,y = sy.symbols('x y')
f, g = sy.symbols('f g', cls = sy.Function)
f(x)
f(x,y)
g(x)
'''

# 7.2
'''
import sympy as sy
i,j = sy.symbols('i j', integer=True)
u,v = sy.symbols('u,v', real=True)
i.is_integer
j*j
(j*j).is_integer
i.is_real
a=sy.I; b=sy.E; c=sy.pi; d=sy.oo
a*a
'''

# 7.2.
'''
import sympy as sy
x,y = sy.symbols('x y')
D = (x+y)*sy.exp(x)*sy.exp(y)
D0=D.subs(x,0)
D0pi=D0.subs(y,sy.pi)
D.subs([(x,0), (y,sy.pi)])
'''

# 7.3. 
'''
import sympy as sy
x = sy.symbols('x', integer=True)
D=x+1/3
D1=x+sy.Rational(1,3)
D2=sy.Rational('0.5')*x
type(D1)
type(D2)
D3=x+sy.S('1/3')
D4=sy.S('1/2')*x
'''

# 7.3. 
'''
import sympy as sy
x,y = sy.symbols('x,y', real=True)
D_s = sy.simplify('(x+y)*exp(x)*cos(y)')
#cosdiff = sy.simplify('cos(x)*cos(y)+sin(x)*sin(y)')
cosdiff = sy.cos(x-y)
cospi4 = cosdiff.subs([(x,sy.pi/2),(y,sy.pi/4)])
cospi4
cospi4.evalf()
cospi4.evalf(6)
sy.N(cospi4,6)
'''

# 7.3. 
'''
import numpy as np
import sympy as sy
x,y = sy.symbols('x,y')
cosdiff = sy.cos(x-y)
func = sy.lambdify((x,y),cosdiff,'numpy')

xn = np.linspace(0,np.pi,13)
zn=func(xn,0.0)

import matplotlib.pyplot as plt
plt.plot(xn,zn,'bo-')
'''

# 7.4. 
'''
import sympy as sy
x,y,u,v = sy.symbols('x,y,u,v')
M = sy.Matrix([ [1,x], [y,1] ])
V = sy.Matrix([ [u],[v] ])
M
V
M*V
M.eigenvects()
M.T
M.det()
M.inv()
M*M.inv()
M[0,1]=u
'''

# 7.5.1.
'''
import sympy as sy
x,y = sy.symbols('x,y')
D = (x+y)*sy.exp(x)*sy.cos(y)
sy.diff(D,x)
D.diff(x)
D.diff(x,y,y)
D.diff(y,2,x)
f = sy.symbols('f', cls=sy.Function)
f(x,y).diff(y,2,x)

D_xyy = sy.Derivative(D,x,y,2)
D_xyy
D_xyy.doit()
'''

# 7.5.2. 
'''
import sympy as sy
x,y = sy.symbols('x,y')
D = (x+y)*sy.exp(x)*sy.cos(y)
sy.integrate(D,y)
D.integrate(y)
D.integrate(x,y)

yD=sy.Integral(D,y)
yD
yD.doit()

sy.integrate(D,(y,0,sy.pi))
D.integrate((y,0,sy.pi))
'''

# 7.5.2.
'''
import sympy as sy
x,y = sy.symbols('x,y')
D=sy.exp(-x**2-y**2)
sy.integrate(D, (x,0,sy.oo), (y,0,sy.oo))

dint = sy.Integral(D, (x,0,sy.oo), (y,0,sy.oo))
dint
dint.doit()

D1 = sy.sqrt(x+sy.sqrt(x**2+1))/x
sy.integrate(D1,x)

D2 = sy.exp(-x)*sy.sin(x**2)/x
sy.integrate(D2,(x,1,sy.oo))

D3 = x/sy.sin(x)
sy.integrate(D3,x)

D4 = sy.exp(sy.sin(x))
sy.integrate(D4,x)
'''

# 7.5.3. 
'''
import sympy as sy
x = sy.symbols('x')

foo = sy.exp(sy.sin(x))
foo_ser = foo.series(x,0,10)
foo_ser
foo_ser.integrate(x)

hoo = (foo-1-x)/x**2
sy.limit(hoo,x,0)

goo = 1/(x-1)
sy.limit(goo,x,1)
sy.limit(goo,x,1,dir='-')
sy.limit(goo,x,1,dir='+')
'''

# 7.6. 
'''
12/3 == 4
import sympy as sy
x,y = sy.symbols('x,y')
ex1 = (x+y)**2
ex2 = x**2 + 2*x*y + y**2
ex1 == ex2
ex1-ex2 == 0

ex1.expand() == ex2
ex1 == ex2.factor()

sy.expand_trig(sy.sin(x+y))
sy.expand(sy.cos(x-y),trig=True)
#sy.expand<tab>
'''

# 7.6.
'''
import sympy as sy
x,y=sy.symbols('x,y')
M=sy.Matrix([[1,x],[y,1]])
A=M*M.inv()
A
A[0,0]=A[0,0].cancel()
A
A[1,1]=A[1,1].cancel()
A

import sympy as sy
x=sy.symbols('x')
C=(x**64-1)/(x-1)
C.cancel()
C.factor()
'''

#7.7.
'''
import sympy as sy
x,y=sy.symbols('x,y')
D=(x+y)*sy.exp(x)*sy.exp(y)
cosdiff=sy.cos(x-y)
lhs=D
rhs=cosdiff
eqn1=sy.Eq(lhs,rhs)
eqn2=lhs-rhs
eqn1
eqn2
'''

#7.7.1. 
'''
import sympy as sy
from sympy.solvers import solveset
from sympy.solvers.solveset import linsolve

x,y=sy.symbols('x,y')
solveset(4*x-3,x)
solveset(3*x**3-16*x**2+23*x-6,x)
quad=x**2-2*x+1
solveset(quad,x)
sy.roots(quad)
solveset(sy.exp(x)-1,x)
solveset(sy.cos(x)-x,x)
'''

#7.7.2.
'''
Eqns=[x+2*y,3*x+4*y-2]
linsolve(Eqns,[x,y])

A=sy.Matrix([[1,2],[2,4]])
b=sy.Matrix([[1],[2]])
linsolve((A,b),[x,y])

A_b=sy.Matrix([[1,2,1],[2,4,1]])
linsolve(A_b,[x,y])
'''


#7.7.2. jms reports a bug
'''
import sympy as sy
x,y=sy.symbols('x,y')
neq=[y**2-x**2-2*x-1, 3*x-y-1]

from sympy.solvers.solveset import linsolve
linsolve(neq,[x,y])

import sympy.solvers as sys
sys.solve(neq,[x,y])
'''

#7.7.3.
'''
import sympy as sy
import sympy.solvers as sys
x,y=sy.symbols('x,y')
eqn1=sy.sqrt(x)-sy.sqrt(y)-1
eqn2=sy.sqrt(x+y)-2
sys.solve([eqn1,eqn2],[x,y])

eq1=sy.root(3*x+1,3)-x-1
eq2=sy.root(3*x-1,3)-x+1
sys.solve(eq1,x)
sys.solve(eq2,x)

sys.solve(eq1,x,check=False)
sys.solve(eq2,x,check=False)
'''

#7.8.
'''
import sympy as sy
from sympy.solvers import dsolve
x=sy.symbols('x')
f=sy.symbols('f',cls=sy.Function)
ode1=f(x).diff(x,2)+4*f(x)
sol1=dsolve(ode1,f(x))

C1,C2=sy.symbols('C1,C2')
fun=sol1.rhs
fund=fun.diff(x)
fun.subs(x,0)
fund.subs(x,0)
psol=sol1.subs([(C2,2),(C1,0)])
'''

#7.8. 
'''
import sympy as sy
from sympy.solvers import dsolve
x=sy.symbols('x')
f=sy.symbols('f',cls=sy.Function)

ode2=sy.sin(f(x))+(x*sy.cos(f(x))+f(x))*f(x).diff(x)
sol2=dsolve(ode2,f(x))
'''

'''
import sympy as sy
from sympy.solvers import dsolve
x=sy.symbols('x')
f=sy.symbols('f',cls=sy.Function)

ode3=x*f(x).diff(x)+f(x)-sy.log(x)*f(x)**2
dsolve(ode3)

ode4 = f(x).diff(x, 2)*x**2 - 4*f(x).diff(x)*x + 6*f(x) - x**3
ode4, dsolve(ode4)

ode5 = f(x).diff(x, 2) + (f(x).diff(x))**2/f(x) + f(x).diff(x)/x
ode5, dsolve(ode5)

ode6 = x*(f(x).diff(x, 2)) + 2*(f(x).diff(x)) + x*f(x)
ode6, dsolve(ode6)
   
ode7 = [f(x).diff(x) - 2*f(x) - g(x), g(x).diff(x) -f(x) - 2*g(x)]
ode7, dsolve(ode7)
 
ode8 = f(x).diff(x) - (x + f(x))**2
ode8, dsolve(ode8)

ode9 = g(x).diff(x) -1 - (g(x))**2
dsolve(ode9)
'''

#7.9.1. 
'''
import sympy as sy
import sympy.plotting as syp
x=sy.symbols('x')
ss='sin(x) and its first three Taylor approximants'
syp.plot(sy.sin(x),x,x-x**3/6, x-x**3/6+x**5/120,
         (x,-4,4),title=ss)
'''

#7.9.2.
'''
import sympy as sy
import sympy.plotting as syp
u=sy.symbols('u')
xc=sy.cos(u)+sy.cos(7*u)/2+sy.sin(17*u)/3
yc=sy.sin(u)+sy.sin(7*u)/2+sy.cos(17*u)/3
fig2=syp.plot_parametric(xc,yc,(u,0,2*sy.pi))
#fig2.save('pic/fig-7-9-2.png')
'''

#7.9.3.
'''
import sympy as sy
import sympy.plotting as syp
x,y=sy.symbols('x,y')
syp.plot_implicit(x**2+x*y+y**2-1,(x,-1.5,1.5),
                  (y,-1.5,1.5))
'''

#7.9.4.
'''
import sympy as sy
import sympy.plotting as syp
x,y=sy.symbols('x,y',real=True)
z=x+sy.I*y
w=sy.cos(z**2).expand(complex=True)
wa=sy.Abs(w).expand(complex=True)
syp.plot_implicit(wa**2-1)
'''

#9.7.5.
'''
import sympy as sy
import sympy.plotting as syp
x,y=sy.symbols('x,y')
#syp.plot_implicit(sy.And(x*y>1,x**2+y**2<4),(x,0,2),
#                  (y,0,2),line_color='lightblue')
syp.plot_implicit(x*y>1,(x,0,2),(y,0,2),line_color='lightblue')
'''

#7.9.6.
'''
import sympy as sy
import sympy.plotting as syp
x,y,u=sy.symbols('x,y,u')
x=u*sy.cos(4*u)
y=u*sy.sin(4*u)
z=u
syp.plot3d_parametric_line(x,y,z,(u,0,10))
'''

#7.9.7.
'''
import sympy as sy
import sympy.plotting as syp
x,y=sy.symbols('x,y')
syp.plot3d(x**2+y**2,x*y,(x,-3,3),(y,-3,3))
'''

#7.9.8.
'''
import sympy as sy
import sympy.plotting as syp

u,v=sy.symbols('u,v')

x=(3+sy.sin(u)+sy.cos(v))*sy.cos(2*u)
y=(3+sy.sin(u)+sy.cos(v))*sy.sin(2*u)
z=2*sy.cos(u)+sy.sin(v)
syp.plot3d_parametric_surface(x,y,z,(u,0,2*sy.pi),(v,0,sy.pi))
'''



